package com.baizhi.hospital.service.impl;

import com.baizhi.hospital.dao.DepartmentDao;
import com.baizhi.hospital.entity.Department;
import com.baizhi.hospital.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {
	@Autowired(required = false)
	private DepartmentDao departmentDao;
	@Override
	@Transactional(readOnly = true)
	public Map selectDepartment(int page, int limit, String departmentName) {
//		参数处理
		if(departmentName != null && !"".equals(departmentName)){
			departmentName = "%"+departmentName+"%";
		}else{
			departmentName = null;
		}

		int offset = (page-1)*limit;
		List<Department> departments = departmentDao.selectDepartmentPageAndVague(offset, limit, departmentName);
		int count = departmentDao.selectCount(departmentName);

//		将查询的数据放入map集合
		Map map = new HashMap();
		map.put("data",departments);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;

	}

	@Override
	@Transactional
	public Map insertDepartment(Department department) {
		Map  map = new HashMap();
		int i = departmentDao.insertDepartment(department);
		if(i > 0 ){
			map.put("isInsert","添加成功");
		}else{
			map.put("isInsert","添加失败");
		}
		return map;
	}

	@Override
	@Transactional
	public Map updateDepartment(Department department) {
		Map map  = new HashMap();
		int i = departmentDao.updateDepartment(department);
		if(i > 0){
			map.put("isUpdate","修改成功");
		}else{
			map.put("isUpdate","修改失败");
		}
		return map;
	}

	@Override
	@Transactional
	public Map deleteDepartment(int id) {
		Map map = new HashMap();
		int i = departmentDao.deleteDepartment(id);
		if(i > 0){
			map.put("isDelete","删除成功");
		}else {
			map.put("isDelete","删除成功");
		}
		return map;
	}


	@Override
	@Transactional
	public Map multiDelete(int[] array) {
		Map map = new HashMap();
		int i = departmentDao.multiDelete(array);
		if(i > 0){
			map.put("isDelete","删除成功");
		}else {
			map.put("isDelete","删除成功");
		}
		return  map;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Department> selectRegisterDepartment() {
		return departmentDao.selectRegisterDepartment();
	}
}
